import Vue from 'vue'
import Vuex from 'vuex'

import {
	area,
	getUserInfo,
	getarea,
} from '@/common/api.js'

import {
	baseUrl
} from '@/common/config.js'

Vue.use(Vuex) // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
	// 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
	state: {

		cityInfo: null,
		// 用户信息
		$userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
		// token: "08eb1658-8ec4-4eab-8ab8-f23359ac4929",

		token: uni.getStorageSync('token') ? uni.getStorageSync('token') : null,
		isLogin: uni.getStorageSync('token') ? true : false,
		p_id: 0,

		areaData: [],

		cityName: null,
		city: 0,

	},
	mutations: {

		setUserInfo: (state, data) => {
			console.log('userInfouserInfo',data);
			uni.setStorageSync('userInfo', data);
			uni.setStorageSync('token', data.token);

			state.$userInfo = uni.getStorageSync('userInfo');
			state.token = uni.getStorageSync('token');
			state.isLogin = true;
		},
		setUserLogout: (state, data) => {
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('token');

			state.isLogin = false;
		},

		setCityInfo(state, data) {
			console.log('datadata', data);
			state.cityName = data.city
			state.cityInfo = data
		},
		setCityId(state, data) {
			state.city = data
		},
		setCityName(state, data) {
			state.cityName = data
		},

	},
	actions: {
		setUserLogout(context) {
			context.commit('setUserLogout')
		},
		getUserInfo(context) {
			console.log('setUserInfo');
			getUserInfo().then(res => {
				console.log('setUserInfo',res.data);
				context.commit('setUserInfo', res.data)
			}).catch(err => {
				return Promise.reject(err)
			})
		},
		setBindParent(context, data) {
			bindParent({
				custom: {
					toast: false
				},
				data: {
					'p_id': data.user_id
				}
			}).then(res => {
				console.log('res', res);
			})
		},

		getAreaData({
			state,
			commit
		}) {
			area().then(res => {
				state.areaData[0] = res;
				area({
					data: {
						'province': 1
					}
				}).then(res => {

					state.areaData[1] = res;
					area({
						data: {
							'province': 1,
							'city': 2
						}
					}).then(res => {
						console.log('北京市111', res);
						state.areaData[2] = res;
					})
				})
			})
		}
	}
})

export default store